﻿using Microsoft.Extensions.Logging;
using XYAC.Framework.WohuaMesExtend.IHI.LocalData.Context;
using XYAC.Framework.WohuaMesExtend.IHI.LocalData.Models;

namespace XYAC.Framework.WohuaMesExtend.IHI.Services
{
    public class YB_LOT_INSPECT_LocalService
    {
        private const string __REMOTE_SYNC_STATE_PENDING = "PENDING";
        private const string __REMOTE_SYNC_STATE_COMPLETE = "COMPLETE";
        private const string __REMOTE_SYNC_STATE_PLANED = "PLANED";

        public YB_LOT_INSPECT_LocalService(LocalDataContext db, ILogger<YB_LOT_INSPECT_LocalService> logger)
        {
            this.Db = db;
            this.Logger = logger;
        }

        public LocalDataContext Db { get; }
        public ILogger<YB_LOT_INSPECT_LocalService> Logger { get; }

        internal IEnumerable<YB_LOT_INSPECT_Local> GetFailedData()
        {
            var snList = this.Db.YB_LOT_INSPECT.Where(T => T.RECEIVE_FLAG == __REMOTE_SYNC_STATE_PLANED && T.RemoteSyncState == false).ToList();
            return snList;
        }

        /// <summary>
        /// 返回当前状态的PlanNo和Sn数据,获取最新条码数据
        /// </summary>
        /// <param name="machineNoList"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        internal IEnumerable<LocalData.Models.YB_LOT_INSPECT_Local> GetSnData(string[] machineNoList, string state = __REMOTE_SYNC_STATE_PENDING)
        {
            var snList = this.Db.YB_LOT_INSPECT.Where(T => machineNoList.Contains(T.EQP) && T.RECEIVE_FLAG == __REMOTE_SYNC_STATE_PENDING).ToList();
            return snList;
        }

        /// <summary>
        /// 返回当前状态的PlanNo和Sn数据,获取最新条码数据
        /// </summary>
        /// <param name="machineNo"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        internal IEnumerable<LocalData.Models.YB_LOT_INSPECT_Local> GetSnData(string machineNo, string state = __REMOTE_SYNC_STATE_PENDING)
        {
            var snList = this.Db.YB_LOT_INSPECT.Where(T => machineNo == T.EQP && T.RECEIVE_FLAG == __REMOTE_SYNC_STATE_PENDING).ToList();
            return snList;
        }

        /// <summary>
        /// 获取本地数据状态为__REMOTE_SYNC_STATE_COMPLETE的数据
        /// </summary>
        /// <param name="machineNo"></param>
        /// <param name="plandate"></param>
        /// <returns></returns>
        internal IEnumerable<LocalData.Models.YB_LOT_INSPECT_Local> GetSnData(string? machineNo, DateTime plandate)
        {
            var snList = this.Db.YB_LOT_INSPECT.Where(T => machineNo == T.EQP && T.RECEIVE_FLAG == __REMOTE_SYNC_STATE_COMPLETE && T.PLAN_DATE.Value.Date == plandate).ToList();
            return snList;
        }

        internal int UpdateFlag(string rEMOTE_SYNC_STATE_COMPLETE, OracleRemoteDB.YB_LOT_INSPECT currentNotifyData)
        {
            int ret = 0;
            using (var trans = this.Db.Database.BeginTransaction())
            {
                try
                {
                    currentNotifyData.RECEIVE_FLAG = rEMOTE_SYNC_STATE_COMPLETE;
                    this.Db.Update(currentNotifyData);
                    var tempRet = this.Db.SaveChanges();
                    trans.Commit();
                    ret = tempRet;
                }
                catch (Exception ex)
                {
                    trans.Rollback();
                    this.Logger.LogError("更新本地缓存中间表数据失败", ex);
                }
            }
            return ret;
        }

        /// <summary>
        /// 更新本地数据库状态
        /// </summary>
        /// <param name="inspectModel"></param>
        /// <returns></returns>
        internal int UpdateInspect(YB_LOT_INSPECT_Local inspectModel, IEnumerable<LocalRecord> preInsertLocalData)
        {
            int ret = 0;
            using (var trans = this.Db.Database.BeginTransaction())
            {
                try
                {
                    this.Db.Update(inspectModel);

                    this.Db.AddRange(preInsertLocalData);

                    var tempRet = this.Db.SaveChanges();



                    trans.Commit();
                    ret = tempRet;
                }
                catch (Exception ex)
                {
                    trans.Rollback();
                    this.Logger.LogError("更新本地缓存中间表数据失败", ex);
                }
            }
            return ret;
        }
    }
}
